<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>ZJCA客户端 管理员操作DEMO</title>

<!--UI式样文件-->
<link href="./style.css" rel="stylesheet" type="text/css">

<!--ZJCA客户端相关的JS文件-->
<script src="../../zjca_cmt/jquery.min.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_cert.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_key.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_com.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_npapi.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_websocket.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_def.js" type="text/javascript"></script>
<script src="../../zjca_cmt/zjcacmt_err.js" type="text/javascript"></script>

<script type="text/javascript">

	/* ZJCA 客户端插件对象 */
	var g_ZjcaCMT = null;
	/* 当前枚举到的设备列表 */
	var g_UsingDeviceArray = null
	/* 当前使用的设备索引 */
	var g_UsingDeviceIndex = -1;
	/* 当前的操作类型 */

	/*
	 *	页面初始化
	 */
	window.onload = function() {
		//创建ZJCA客户端控件对象
		g_ZjcaCMT = new zjca_CMT(onCallbackKeyEvent, null);
		
		//设置客户端交互协议，在初始之前设置生效
		g_ZjcaCMT.setProtocol(PROTOCOL_HTTPS);
		
		//初始化ZJCA控件对象
		var resp = g_ZjcaCMT.init(ZJCA_PRODUCT_CMT, 1);
		if (resp.code != 0) {
			alert("初始化ZJCA客户端失败！\n错误信息：" + resp.msg);
		}else {
			//显示版本信息
			resp = g_ZjcaCMT.getVersion();
			if (resp.code == 0) {
				document.getElementById('txtInfo').innerHTML = "客户端版本：" + resp.res;
			}else {
				document.getElementById('txtInfo').innerHTML = "客户端版本：未知";
			}
			
			////////////////////////////测试用户设置////////////////////////////////
			//var auto_run = g_ZjcaCMT.getUserSetting(ZJCA_OPTION_AUTORUN);
			//var auto_update = g_ZjcaCMT.getUserSetting(ZJCA_OPTION_CHECKUPDATE);
			//var auto_showpage = g_ZjcaCMT.getUserSetting(ZJCA_OPTION_SHOWPAGE);
			//			
			//g_ZjcaCMT.putUserSetting(ZJCA_OPTION_AUTORUN, 1);
			//g_ZjcaCMT.putUserSetting(ZJCA_OPTION_CHECKUPDATE, 0);
			//g_ZjcaCMT.putUserSetting(ZJCA_OPTION_SHOWPAGE, 1);
			//
			//var auto_run1 = g_ZjcaCMT.getUserSetting(ZJCA_OPTION_AUTORUN);
			//var auto_update1 = g_ZjcaCMT.getUserSetting(ZJCA_OPTION_CHECKUPDATE);
			//var auto_showpage1 = g_ZjcaCMT.getUserSetting(ZJCA_OPTION_SHOWPAGE);
			//			
			//g_ZjcaCMT.putUserSetting(ZJCA_OPTION_AUTORUN, auto_run.res);
			//g_ZjcaCMT.putUserSetting(ZJCA_OPTION_CHECKUPDATE, auto_update.res);
			//g_ZjcaCMT.putUserSetting(ZJCA_OPTION_SHOWPAGE, auto_showpage.res);
			////////////////////////////测试用户设置////////////////////////////////
			
			
			//获取设备列表
			resp = g_ZjcaCMT.getKeyList(false);
			if (resp.code != 0) {
				ShowErrorMessage("g_ZjcaCMT.getKeyList()失败!", resp.code);	
			}else {
				FillDeviceList(resp.res);
			}
		}
	}
	/*
	 *	页面关闭
	 */
	window.onunload = function() {
		/* 释放接口 */
		if (g_ZjcaCMT) {
			g_ZjcaCMT.finaled();
		}
	}
	/*
	 *  列出设备
	 */
	function FillDeviceList(keyArray) {			
		// 删除老的选项
		var selKeyCtrl = document.getElementById('selKeys');
		var itemCount = selKeyCtrl.options.length;
		for (var n = 0; n < itemCount; n++) {
			selKeyCtrl.options.remove(0);
		}
		
		// 保存当前选择的设备
		var selDeviceSN;
		if (g_UsingDeviceIndex >= 0) {			
			selDeviceSN = g_UsingDeviceArray[g_UsingDeviceIndex].getSN();
		}
		
		// 更新列表
		g_UsingDeviceArray = keyArray;
		if (g_UsingDeviceArray && g_UsingDeviceArray.length > 0) {
			selKeyCtrl.options.add(new Option('请选择UKey设备', -1));

			for (var n = 0; n < g_UsingDeviceArray.length; n++) {
				var keySN = g_UsingDeviceArray[n].getSN();
				var keyLabel = g_UsingDeviceArray[n].getLabel();
				var text = keyLabel + "(" + keySN + ")";
				selKeyCtrl.options.add(new Option(text, n));
			}
		}
		else {
			selKeyCtrl.options.add(new Option('未发现任何ZJCA UKey设备', -1));
		}

		// 设置选中的设备
		if (g_UsingDeviceArray) {
			var i = 0;
			if (g_UsingDeviceIndex >= 0) {
				for (i = 0; i < g_UsingDeviceArray.length; i++) {
					if (selDeviceSN == g_UsingDeviceArray[i].getSN()) {
						document.form1.selKeys.options[i].selected = true;
						g_UsingDeviceIndex = i;
						break;
					}
				}
			}
			if (i == g_UsingDeviceArray.length) {
				document.form1.selKeys.options[0].selected = true;
				OnSelKeyChanged();
			}
		}
	}
	/*
	 *  选择设备，列出选中设备的基本信息
	 */
	function OnSelKeyChanged() {
		g_UsingDeviceIndex = document.getElementById("selKeys").value;		
	}
	/*
	 *	生成请求码
	 */
	function onBtnGenReqCode(type) {
		if (g_UsingDeviceIndex < 0) {
			alert("请选择设备！");
			return;
		}
		if (g_UsingDeviceIndex >= g_UsingDeviceArray.length) {
			alert("选择的设备错误！");
			return;
		}
		if (g_ZjcaCMT == null) {
			alert("g_ZjcaCMT is NULL!");
			return;
		}
		
		//生成授权请求码
		var resp = g_ZjcaCMT.genAuthReq(g_UsingDeviceIndex, 	//设备序号
									    type 					//0x01：设备初始化；0x02：解锁
										);
		//请求码生成成功
		if (resp.code == 0) {
			if (0x01 == type) {		// 初始化设备
				document.form1.txtFormatReq.value = resp.res;
			}else if (0x02 == type) {	// 解锁设备
				document.form1.txtUnlockReq.value = resp.res;
			}			
		}else {	//生成请求码失败
			ShowErrorMessage("g_ZjcaCMT.genAuthReq()失败!", resp.code);		
		}
	}
	/*
	 *	格式化KEY
	 */
	function onBtnFormat() {
		if (g_UsingDeviceIndex < 0) {
			alert("请选择设备！");
			return;
		}
		if (g_UsingDeviceIndex >= g_UsingDeviceArray.length) {
			alert("选择的设备错误！");
			return;
		}
		var authCode = document.form1.txtFormatAuth.value;
		if (authCode.length == 0) {
			alert("授权码不能为空！");
			return;			
		}
		
		// 格式化
		var resp = g_ZjcaCMT.initKey(g_UsingDeviceIndex,//设备序号
									 authCode, 			//授权码
									 "",				//初始PIN码，传空表示弹出PIN设置对话框
									 ""					//设备标签，空表示保持原有的标签不变
									 );
		if (0 == resp.code) {	//格式化成功	
			alert("设备初始化成功！");				
		}else {			//格式化失败
			if (0x81000007 != zjca_GetErrHexCode(resp.code)) { //如果不是用户取消操作,则显示错误信息
				ShowErrorMessage("g_ZjcaCMT.initKey()失败!", resp.code);
			}				
		}
	}	
	/*
	 *	解锁KEY
	 */
	function onBtnUnlock() {	
		if (g_UsingDeviceIndex < 0) {
			alert("请选择设备！");
			return;
		}
		if (g_UsingDeviceIndex >= g_UsingDeviceArray.length) {
			alert("选择的设备错误！");
			return;
		}
		var authCode = document.form1.txtUnlockAuth.value;
		if (authCode.length == 0) {
			alert("授权码不能为空！");
			return;			
		}
		
		// 解锁
		var resp = g_ZjcaCMT.unlockPin(g_UsingDeviceIndex, 	//设备序号
									   authCode, 			//授权码
									   ""					//新PIN码，传空表示弹出PIN设置对话框
										);	
		if (0 == resp.code) {	//解锁成功	
			alert("解锁成功！");				
		}else {			//解锁失败
			if (0x81000007 != zjca_GetErrHexCode(resp.code)) { //如果不是用户取消操作,则显示错误信息
				ShowErrorMessage("g_ZjcaCMT.unlockPin()失败!", resp.code);
			}				
		}
	}
	//	设备事件响应函数
	function onCallbackKeyEvent(type, index, name) {
		// 重新获取设备列表
		var resp = g_ZjcaCMT.getKeyList(false);	
		if (resp.code == 0) {
			FillDeviceList(resp.res);
		}			
	}
	//	显示错误信息函数
	function ShowErrorMessage(msg, code) {
		var errCode = zjca_GetErrHexCode(code);
		var errTxt = zjca_GetErrTxt(code);
	
		var errMsg = msg;
		errMsg += "\n";
		errMsg += "错误代码：";
		errMsg += errCode;
		errMsg += "\n";
		errMsg += "错误信息：";
		errMsg += errTxt;
		alert(errMsg);
	}	
</script>

<meta http-equiv="Access-Control-Allow-Origin" content="*.zjca.com.cn">
<meta name="GENERATOR" content="MSHTML 11.00.9600.17728">
<body screen_capture_injected="true">
	<br>
	<br>
	<h2 align="center">ZJCA客户端管理员操作DEMO</h2>
	<br>
	<br>
	<form name="form1" action="">
		<fieldset>
			<legend>帮助说明：</legend>
			<td>管理员操作包含“格式化”和“解锁”，主要流程说明如下：</td><br>
			<td>1、新建JS对象zjca_CMT；</td><br>
			<td>2、调用JS API：zjca_CMT.init()初始化；</td><br>
			<td>3、待初始化函数返回后，调用JS API：zjca_CMT.getKeyList()获取设备列表；</td><br>
			<td>4、选择一个设备；</td><br>
			<td>5、生成授权请求码，调用JS API：zjca_CMT.genAuthReq()；</td><br>
			<td>6、生成授权请求码消息返回后，将得到的请求码发送给授权服务器，并返回授权码；</td><br>
			<td>7、调用对应的操作API：“格式化”调用zjca_CMT.initKey()、“解锁”调用zjca_CMT.unlockPin()；</td><br>
			<td>8、在页面关闭时，调用zjca_CMT.finaled()反初始化。</td><br>
		</fieldset>
		<br> <br>
		<fieldset>
			<legend>客户端信息：</legend>
			<table>
				<colgroup>
					<col class="col1">
					<col class="col2">
					<col class="col3">
				</colgroup><tbody>
					<tr>
						<td></td>
						<td></td>
						<td></td>
					</tr>
					<tr>
						<td><label name="txtInfo" id="txtInfo" width="500" >未检测到ZJCA客户端程序！</label></td>
					</tr>
				</tbody>
			</table>
		</fieldset>
		<br> <br>
		<fieldset>
			<legend>密码设备：</legend>
			<table>
				<colgroup>
					<col class="col1">
					<col class="col2">
					<col class="col3">
				</colgroup><tbody>
					<tr>
						<td></td>
						<td></td>
						<td></td>
					</tr>
					<tr>
						<td>请选择Key：</td>
						<td colspan="2"><select id="selKeys" name="selKeys" onchange="OnSelKeyChanged()"></select></td>
					</tr>
				</tbody>
			</table>
		</fieldset>
		<br> <br>
		<fieldset>
			<legend>格式化：</legend>
			<td>授权请求码：</td><br>
			<TEXTAREA name="txtFormatReq" rows="5" cols="70"></TEXTAREA><br>
			<tr>
				<td>
					<input name="btnGenFormatReq" class="keyBtn" onclick="onBtnGenReqCode(1)" type="button" value="生成授权请求码">
					<br>
					<br>
					<td>(请将请求码发送给授权服务器，并将返回的授权码填入下面输入框中，再进行相应的操作!)</td>
					<br>
				</td>
			</tr>
			<br>
			<td>请输入授权码：</td><br>
			<tr>
				<TEXTAREA name="txtFormatAuth" rows="5" cols="70"></TEXTAREA><br>
				<td>
					<input name="btnFormat" class="keyBtn" onclick="onBtnFormat()" type="button" value="格式化"><br>
				</td>
			</tr>
		</fieldset>		
		<br> <br>
		<fieldset>
			<legend>解锁：</legend>
			<td>授权请求码：</td><br>
			<TEXTAREA name="txtUnlockReq" rows="5" cols="70"></TEXTAREA><br>
			<tr>
				<td>
					<input name="btnGenUnlockReq" class="keyBtn" onclick="onBtnGenReqCode(2)" type="button" value="生成授权请求码">
					<br>
					<br>
					<td>(请将请求码发送给授权服务器，并将返回的授权码填入下面输入框中，再进行相应的操作!)</td>
					<br>
				</td>
			</tr>
			<br>
			<td>请输入授权码:</td><br>
			<tr>
				<TEXTAREA name="txtUnlockAuth" rows="5" cols="70"></TEXTAREA><br>
				<td>
					<input name="btnUnlock" class="keyBtn" onclick="onBtnUnlock()" type="button" value="解锁"><br>
				</td>
			</tr>
		</fieldset>
		<br> <br>
		<div id="result"></div>
	</form>
</body>
</html>